#include <iostream>
#include <fstream>
#include <string>
#include "bundle.h"

int example_pack(int argc, char *argv[]) {
    std::cout << "argv[1] 是原始文件路径名称" << std::endl;
    std::cout << "argv[2] 是压缩包名称" << std::endl;

    if (argc < 3) return -1;

    std::string ifilename = argv[1];
    std::string ofilename = argv[2];

    std::ifstream ifs;
    ifs.open(ifilename, std::ios::binary);

    ifs.seekg(0, std::ios::end);
    size_t fsize = ifs.tellg();    //   获取偏移量
    ifs.seekg(0, std::ios::beg);
    std::string body;
    body.resize(fsize);
    ifs.read(&body[0], fsize);

    std::string packed = bundle::pack(bundle::LZIP, body);

    std::ofstream ofs;
    ofs.open(ofilename, std::ios::binary);
    ofs.write(&packed[0], packed.size());

    ifs.close();
    ofs.close();

    return 0;
} 

int example_unpack(int argc, char *argv[]) {
    if (argc < 3) {
        std::cout << "argv[1] 是压缩包名称" << std::endl;
        std::cout << "argv[2] 是解压后文件的名称" << std::endl;
        return -1;
    }
    std::string ifilename = argv[1]; // 压缩包名称
    std::string ofilename = argv[2]; // 解压后文件名称

    std::ifstream ifs(ifilename, std::ios::binary);
    ifs.seekg(0, std::ios::end);
    size_t size = ifs.tellg();
    ifs.seekg(0, std::ios::beg);
    std::string body;
    body.resize(size, 0);
    ifs.read(&body[0], size);
    ifs.close();

    std::string unpacked = bundle::unpack(body);
    
    std::ofstream ofs;
    ofs.open(ofilename, std::ios::binary);
    ofs.write(&unpacked[0], unpacked.size());
    ofs.close();
    return 0;
}

int main(int argc, char *argv[]) {
    // example_pack(argc, argv);
    example_unpack(argc, argv);
    return 0;
}